//引入less样式
require('../less/material.less')

//引入样式
require('weui')
let weui = require('weui.js')






//文档加载事件
document.ready(function () {

    //判断用户是否登录
    until.isLogin()


    //获取
    const returnkey = document.querySelector('#returnkey')
    const btn = document.querySelector('#btn')
    const userImgIpt = document.querySelector('#userImgIpt')
    const headimg = document.querySelector('#headimg')
    const showPicker = document.querySelector('#showPicker')
    const showbirthdayPicker = document.querySelector('#showbirthdayPicker')
    const showprovincePicker = document.querySelector('#showprovincePicker')
    const showcityPicker = document.querySelector('#showcityPicker')
    const text = document.querySelector('#text')
    const curLength = document.querySelector('#curLength')
    const js_input = document.querySelector('#js_input')


    //点击事件
    returnkey.addEventListener('click', function () {
        location.href = "./mine.html"
    })

    //点击 保存数据
    btn.addEventListener('click', function () {

        const params = {
            imgurl: imgSrc,
            nickname: js_input.value,
            gender: showPicker.textContent,
            birthday: showbirthdayPicker.textContent,
            address: [showprovincePicker.textContent, showcityPicker.textContent],
            sign: text.value,
            userId: localStorage.getItem('userId')
        }
        console.log(params);


        //发送请求
        let nickname = ''
        $http.post('/users/userEdit', params, function (res) {
            if (res.status === 0) {
                until.showToast('success', '保存成功')

                setTimeout(function () {
                    location.href = "./mine.html"
                }, 2000)

            }


        })
    })

    //数据回填
    function setUserData() {
        const userInfo = JSON.parse(localStorage.getItem('userInfo'))

        //头像
        if (userInfo.imgurl) headimg.src = 'http://139.9.177.51:8099' + userInfo.imgurl

        //昵名
        js_input.value = userInfo.nickname

        //性别
        if (userInfo.gender) showPicker.textContent = userInfo.gender

        //生日
        if (userInfo.birthday) showbirthdayPicker.textContent = userInfo.birthday.split('T')[0]

        //省份//城市
        if (userInfo.address) {
            showprovincePicker.textContent = userInfo.address.split(',')[0]
            showcityPicker.textContent = userInfo.address.split(',')[1]
        }

        //签名
        text.textContent = userInfo.sign
    }
    setUserData()

    //改变事件//上传图片
    let imgSrc = ''
    userImgIpt.addEventListener('change', function () {
        // 得到上传的文件列表
        const fileList = this.files;

        // 得到文件对象
        let file = fileList[0];

        //调用请求
        $updateFile('/users/upload', 'imgurl', file, function (res) {
            if (res.status === 0) {
                imgSrc = res.data
                headimg.src = 'http://139.9.177.51:8099' + res.data
            }
        })
    })


    //点击头像切换
    headimg.addEventListener('click', function () {
        userImgIpt.click();
        
    })




    //性别
    showPicker.addEventListener('click', function () {
        weui.picker([{
            label: '男',
            value: 1
        }, {
            label: '女',
            value: 0
        }, {
            label: '其他',
            value: 3
        }], {
            onConfirm: function (result) {
                showPicker.textContent = result[0].label;
            },
            title: '性别选择器'
        });
    });

    //生日
    showbirthdayPicker.addEventListener('click', function () {
        weui.datePicker({
            start: 1970,
            end: new Date().getFullYear(),
            onConfirm: function (result) {
                let arr = result.map(function (item) {

                    return until.polishing(item.value)
                }).join('-')
                showbirthdayPicker.textContent = arr

            },
            title: '生日选择器'
        });
    });



    //获取省
    //定义变量接受省
    let province = null
    function getProvince() {
        $http.get('/address/province', function (res) {
            //省处理
            if (res.status === 0) {
                province = res.data.map(function (item) {
                    return {
                        label: item.name,
                        value: item.addressId
                    }
                })


            }


        })
    }
    getProvince()

    //省

    let city = null
    showprovincePicker.addEventListener('click', function () {
        weui.picker(province, {
            onConfirm: function (result) {

                showprovincePicker.textContent = result[0].label


                //获取当前省的id
                let id = result[0].value;

                //获取城市
                $http.get(`/address/city/${id}`, function (c) {
                    city = c.data.map(function (item) {
                        return {
                            label: item.name,
                            value: item.addressId
                        }
                    })
                    console.log(city);


                })

            },
            title: '省选择'
        });
    });
    //城市
    showcityPicker.addEventListener('click', function () {
        weui.picker(city, {
            onConfirm: function (result) {
                showcityPicker.textContent = result[0].label;

            },
            title: '城市选择'
        });
    });

    //个性签名
    //输入字数
    text.addEventListener('input', function () {
        curLength.textContent = this.value.length
    })

})